%%%-------------------------------------------------------------------
%%% File    : rdf_store_SUITE.erl
%%% Author  :  <vjache>
%%% Description : 
%%%
%%% Created : 26 May 2009 by  <vjache>
%%%-------------------------------------------------------------------
-module(rdf_store_SUITE).

-compile(export_all).

-include_library("ct.hrl").

-include("../include/rdf_spalg.hrl"). 
-include("../include/rdf_record.hrl").
-include("../include/rdf_rs.hrl"). 
-include("../include/test.hrl"). 

%%--------------------------------------------------------------------
%% Function: suite() -> Info
%% Info = [tuple()]
%%--------------------------------------------------------------------
suite() ->
    [{timetrap,{seconds,3600}}].

%%--------------------------------------------------------------------
%% Function: init_per_suite(Config0) ->
%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%%--------------------------------------------------------------------
init_per_suite(Config) ->
	create_rdf_store(),
	case init:get_argument(dfs) of
		error ->
			ct:fail({no_dfs_arg});
		{ok,DeepList} ->
			OutDir=lists:flatten(DeepList),
			Sett=mega_data:new(20000,_Children=4,_Fri=5,_Intersts=3,?CONFIG(data_dir,Config),OutDir),
			mega_data:gen(Sett,[node()]),
			Config
 	end.
%% 	Config.

%%--------------------------------------------------------------------
%% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
%% Config0 = Config1 = [tuple()]
%%--------------------------------------------------------------------
end_per_suite(_Config) ->
    ok.

%%--------------------------------------------------------------------
%% Function: init_per_group(GroupName, Config0) ->
%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
%% GroupName = atom()
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%%--------------------------------------------------------------------
init_per_group(_GroupName, Config) ->
    Config.

%%--------------------------------------------------------------------
%% Function: end_per_group(GroupName, Config0) ->
%%               void() | {save_config,Config1}
%% GroupName = atom()
%% Config0 = Config1 = [tuple()]
%%--------------------------------------------------------------------
end_per_group(_GroupName, _Config) ->
    ok.

%%--------------------------------------------------------------------
%% Function: init_per_testcase(TestCase, Config0) ->
%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
%% TestCase = atom()
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%%--------------------------------------------------------------------
init_per_testcase(_TestCase, Config) ->
    Config.

%%--------------------------------------------------------------------
%% Function: end_per_testcase(TestCase, Config0) ->
%%               void() | {save_config,Config1}
%% TestCase = atom()
%% Config0 = Config1 = [tuple()]
%%--------------------------------------------------------------------
end_per_testcase(_TestCase, _Config) ->
    ok.

%%--------------------------------------------------------------------
%% Function: groups() -> [Group]
%% Group = {GroupName,Properties,GroupsAndTestCases}
%% GroupName = atom()
%% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
%% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
%% TestCase = atom()
%% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
%% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
%%              repeat_until_any_ok | repeat_until_any_fail
%% N = integer() | forever
%%--------------------------------------------------------------------
groups() ->
    [].

%%--------------------------------------------------------------------
%% Function: all() -> GroupsAndTestCases | {skip,Reason}
%% GroupsAndTestCases = [{group,GroupName} | TestCase]
%% GroupName = atom()
%% TestCase = atom()
%% Reason = term()
%%--------------------------------------------------------------------
all() -> 
    [upload,person_info].

%%--------------------------------------------------------------------
%% Function: TestCase() -> Info
%% Info = [tuple()]
%%--------------------------------------------------------------------
upload() -> 
    [].

%%--------------------------------------------------------------------
%% Function: TestCase(Config0) ->
%%               ok | exit() | {skip,Reason} | {comment,Comment} |
%%               {save_config,Config1} | {skip_and_save,Reason,Config1}
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%% Comment = term()
%%--------------------------------------------------------------------
upload(Config) -> 
	Sess=rdf_store:open("TestStore"),
	{Nodes,_}=svc_rrs:get_nodes(),
	case init:get_argument(dfs) of
		error ->
			ct:fail({no_dfs_arg});
		{ok,DeepList} ->
			OutDir=lists:flatten(DeepList),
%% 			Proms=[rpc:async_call(Node, rdf_store, upload, 
%% 							[Sess,{log,OutDir++"/mega_data_"++atom_to_list(Node)++".LOG"}])
%% 			||Node<-Nodes,Node/=node()],
			ok=rdf_store:upload(Sess,{log,OutDir++"/mega_data_"++atom_to_list(node())++".LOG"})
%% 	,
%% 			case lists:filter(fun({badrpc,_})->
%% 								 true;
%% 							(_) ->
%% 								 false
%% 						 end, [rpc:yield(Prom)||Prom<-Proms]) of
%% 				[] ->
%% 					ok;
%% 				Bads ->
%% 					ct:fail({some_upload_failed,Bads})
%% 			end
	end,
	io:format("FFFFFFFFFFFFFFFFF ~p~n", [now()]),
%% 	%%Upload addresses, companies, countries
%% 	AddrFile=?CONFIG(data_dir,Config)++"addresses.tup",
%% 	CompFile=?CONFIG(data_dir,Config)++"companies.tup",
%% 	CountrFile=?CONFIG(data_dir,Config)++"countries.tup",
%% 	ok=rdf_store:upload(Sess, {tup,AddrFile}),
%% 	ok=rdf_store:upload(Sess, {tup,CompFile}),
%% 	ok=rdf_store:upload(Sess, {tup,CountrFile}),
    ok.

person_info() -> 
    [].
person_info(_Config) ->
	S={onto,100},
	Sc=subjCh,
	Age=age,
%% 	ParentName=parName,
	Name=name,
	Family=family,
%% 	FriendId=friend,
	Interest=interest,
%% 	VasyaLit={'$lit',"Vasya",string},
	Ss=rdf_sol_builder:new_from_quad(tp(S,{onto,"hasChild"},Sc), S),
	Ss1=rdf_sol_builder:join_lk(Ss, [
									 tp(Sc,{onto,"hasName"},Name),
									 tp(Sc,{onto,"hasFamilyName"},Family),
									 tp(Sc,{onto,"hasAge"},Age),
									 tp(Sc,{onto,"hasInterest"},Interest)
									], Sc),
	SsResult=rdf_sol_builder:project(Ss1, [Name,Family,Age,Interest]),
	Pg=valg2pgt:fold(SsResult#solset.expr, pipe_console:new()),
	ct:log(info,lists:flatten(valg2pgt:format(Pg))),
	Pid=pgt:new(Pg),
	ok=util:wait_finish(Pid).

tp(S,P,O) ->
	{?QUAD_TAG_SA,S,P,O,'_'}.
%%-------------------------------------------------------------------
%% Helper functions
%%-------------------------------------------------------------------
create_rdf_store() ->
	lists:foreach(fun(Tag) ->
						  ?CT_LOG_INFO(
							"[PID:~p] Create vector ~p: ~p~n",
							[
							 self(),
							 Tag,
%% 							 svc_vms:create_vect(Tag, list_to_atom("rdf_"++atom_to_list(Tag)), [list_to_atom("quad_"++atom_to_list(Tag)),10000,quad])
							 svc_vms:create_vect(Tag, list_to_atom("rdf_"++atom_to_list(Tag)), [])
							]
						  )
				  end, ?ALL_TAGs).


